/**
 * Created by xc on 15/12/22.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbDocTypeModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`type_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '模板类型ID',
  //`category` int(1) NOT NULL DEFAULT '1' COMMENT '分类: 1行业; 2风格',
  //`cat_name` varchar(20) DEFAULT NULL COMMENT '分类名',
  //`type_index` int(4) NOT NULL DEFAULT '0' COMMENT '模版类型编号',
  //`type_name` varchar(20) NOT NULL COMMENT '模版类型名',
  //`icon` text COMMENT '图标',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbDocTypeModel.prototype = new ItbBaseModel();

// 取得表名
ItbDocTypeModel.prototype.getTableName = function(){
    return 'itb_doc_type';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbDocTypeModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'type_id=' + this.type_id;
};

// 根据文档id取所有type名
ItbDocTypeModel.prototype.getTypeByCat = function(category, callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' SELECT * ' +
            ' FROM itb_doc_type t ' +
            ' WHERE t.del_flg=0 AND t.category='+category+
            ' ORDER BY t.type_index ';

        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
};

// 导出
module.exports = ItbDocTypeModel;